package com.cfpl.admin_service.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cfpl.admin_service.VO.AvailableFertilizer;
import com.cfpl.admin_service.VO.FertilizationDetails;
import com.cfpl.admin_service.service.FertilizerAcquireRecordService;
import com.cfpl.admin_service.service.FertilizerService;
import com.cfpl.project_common.context.BaseContext;
import com.cfpl.project_common.pojo.Admin;
import com.cfpl.project_common.pojo.Fertilizer;
import com.cfpl.project_common.pojo.FertilizerAcquireRecord;
import com.cfpl.project_common.pojo.FertilizerUsageRecords;
import com.cfpl.project_common.result.BaseResult;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: LYP
 * @Date: 2023/10/17 - 14:38
 * @Description: com.cfpl.admin_service.controller
 * @version: 1.0
 */
@RestController
@RequestMapping("/fertilizer")
@Api(tags = "肥料相关接口")
@Slf4j
public class FertilizerController {

    @Autowired
    private FertilizerService fertilizerService;
    @Autowired
    private FertilizerAcquireRecordService fertilizerAcquireRecordService;

    /**
     * 分页查询肥料信息
     * @param currentPage currentPage
     * @param pageSize pageSize
     * @return page
     */
    @GetMapping("/pageQueryFertilizer")
    @ApiOperation("分页查询肥料信息")
    public BaseResult<Page<Fertilizer>> pageQueryFertilizer(int currentPage, int pageSize){
        Page<Fertilizer> page = fertilizerService.page(new Page<>(currentPage, pageSize));
        return BaseResult.ok(page);
    }

    /**
     * 添加肥料信息
     * @param fertilizer fertilizer
     */
    @ApiOperation("添加肥料")
    @PostMapping("/insertFertilizer")
    public BaseResult insertFertilizer(Fertilizer fertilizer){
        fertilizerService.insertFertilizer(fertilizer);
        return BaseResult.ok();
    }

    /**
     * 根据id查询
     * @param fertilizerId id
     * @return 肥料信息
     */
    @GetMapping("/queryFertilizer/{id}")
    @ApiOperation("根据id查询肥料")
    public BaseResult<Fertilizer> queryFertilizer(@PathVariable("id") Long fertilizerId){
        Fertilizer fertilizer = fertilizerService.queryFertilizerById(fertilizerId);
        return BaseResult.ok(fertilizer);
    }

    /**
     * 更新肥料信息
     * @param fertilizer 肥料对象
     */
    @ApiOperation("更新肥料信息")
    @PostMapping("/updateFertilizer")
    public BaseResult updateFertilizer(@RequestBody Fertilizer fertilizer){
        fertilizerService.updateFertilizer(fertilizer);
        return BaseResult.ok();
    }

    /**
     * 删除肥料信息
     * @param fertilizerIds 编号集合
     */
    @ApiOperation("批量删除肥料信息")
    @PostMapping("/removeFertilizer")
    public BaseResult removeFertilizer(@RequestParam List<Long> fertilizerIds){
        fertilizerService.removeFertilizer(fertilizerIds);
        return BaseResult.ok();
    }

    /**
     * 添加领取肥料记录
     * @param fertilizerAcquireRecord 记录信息
     */
    @PostMapping("/insertFertilizerAcquireRecord")
    @ApiOperation("肥料领取接口,可不用传入使用数量")
    public BaseResult insertFertilizerAcquireRecord(@RequestBody FertilizerAcquireRecord fertilizerAcquireRecord){
        fertilizerService.addAcquireFertilizerRecord(fertilizerAcquireRecord);
        return BaseResult.ok();
    }

    /**
     * 添加肥料使用记录
     * @param fertilizerUsageRecords 记录对象
     */
    @PostMapping("/insertFertilizerUsageRecord")
    @ApiOperation("添加肥料使用记录")
    public BaseResult insertFertilizerUsageRecord(@RequestBody FertilizerUsageRecords fertilizerUsageRecords){
        fertilizerService.insertFertilizerUsageRecord(fertilizerUsageRecords);
        return BaseResult.ok();
    }

    /**
     * 根据肥料领取记录查询本次所有的施肥记录
     * @param id 记录ID
     * @return 施肥记录
     */
    @GetMapping("/queryUsageRecordByAcquireRecordId/{id}")
    @ApiOperation("根据肥料领取记录查询本次所有的施肥记录")
    public BaseResult queryUsageRecordByAcquireRecord(@PathVariable("id") Long id){
        List<FertilizationDetails> detailsList = fertilizerAcquireRecordService.queryUsageRecordByAcquireRecord(id);
        return BaseResult.ok(detailsList);
    }

    /**
     * 查询当前培育人员可使用的肥料信息列表
     * @return 可使用的肥料信息列表
     */
    @ApiOperation("查询当前培育人员可使用的肥料信息接口")
    @GetMapping("/queryAvailableFertilizerList")
    public BaseResult<List<AvailableFertilizer>> queryAvailableFertilizerList(){
        List<AvailableFertilizer> fertilizerList = fertilizerAcquireRecordService.queryAvailableFertilizerList(BaseContext.getCurrentUserId());
        return BaseResult.ok(fertilizerList);
    }







}
